<?php

/**
 * This is the model class for table "project".
 *
 * The followings are the available columns in table 'project':
 * @property integer $id
 * @property string $name
 * @property string $subtitle
 * @property string $description
 * @property string $brief
 * @property string $location
 * @property string $address_details
 * @property string $city
 * @property string $province
 * @property string $country
 * @property string $building_features
 * @property string $features
 * @property string $amenities
 * @property string $latlng
 *
 * The followings are the available model relations:
 * @property Picture[] $pictures
 * @property Property[] $properties
 * @property Testimonials[] $testimonials
 */
class Project extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Project the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'project';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, latlng', 'required'),
			array('views', 'numerical', 'integerOnly'=>true),
			array('name, latlng', 'length', 'max'=>100),
			array('subtitle, description, brief, location, address_details, city, province, country, building_features, features, amenities', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, subtitle, description, brief, location, address_details, city, province, country, building_features, features, amenities, latlng', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'pictures' => array(self::HAS_MANY, 'Picture', 'Project_id'),
			'properties' => array(self::HAS_MANY, 'Property', 'Project_id'),
			'testimonials' => array(self::HAS_MANY, 'Testimonials', 'Project_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'subtitle' => 'Subtitle',
			'description' => 'Description',
			'brief' => 'Brief',
			'location' => 'Location',
			'address_details' => 'Address Details',
			'city' => 'City',
			'province' => 'Province',
			'country' => 'Country',
			'building_features' => 'Building Features',
			'features' => 'Features',
			'amenities' => 'Amenities',
			'latlng' => 'Latlng',
			'views' => 'Views',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('subtitle',$this->subtitle,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('brief',$this->brief,true);
		$criteria->compare('location',$this->location,true);
		$criteria->compare('address_details',$this->address_details,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('country',$this->country,true);
		$criteria->compare('building_features',$this->building_features,true);
		$criteria->compare('features',$this->features,true);
		$criteria->compare('amenities',$this->amenities,true);
		$criteria->compare('latlng',$this->latlng,true);
		$criteria->compare('views',$this->views);
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function initsort()
	{
		/* Sort on Model's columns */
        $sort = new CSort;
        $sort->attributes = array(
			'name'
        );

        return new CActiveDataProvider($this, array(
			'criteria'=>array(
								'select'=>'*',
								'order' => 'name',
								),
            'sort'=>$sort, /* Needed for sort */
        ));
	}
	
	public function scopes()
    {
        return array(
            'mostViewed'=>array(
                'order'=>'`views` DESC',
                'limit'=>3,
            ),
        );
    }
}